/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package WebBrower_DAO;

import WebBrowser_DTO.Website_DTO;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class Website_DAO {

    public int Insert(Website_DTO obj) {
        int ID = -1;
        try {

            SqlDataHelper helper = new SqlDataHelper();
            helper.OpenConnection();
            List<ParamDTO> params = new ArrayList<ParamDTO>();

            ParamDTO TenWS = new ParamDTO("TenWebsite",
                    DataTypeParam.StringType, obj.getTenWebsite());

            ParamDTO WSLink = new ParamDTO("WebsiteLink",
                    DataTypeParam.StringType, obj.getWebsiteLink());
            params.add(TenWS);
            params.add(WSLink);
            ID = helper.excuteNonQueryInsert("sp_Website_Insert", params,
                    "sp_Website_MaxID");

            helper.CloseConnection();
        } catch (Exception ex) {
        }
        return ID;
    }

    public boolean Update(Website_DTO obj) {
        boolean isSuccess = false;
        try {
            SqlDataHelper helper = new SqlDataHelper();
            helper.OpenConnection();
            List<ParamDTO> listParams = new ArrayList<ParamDTO>();

            ParamDTO DanhMucID = new ParamDTO("WebsiteID",
                    DataTypeParam.IntegerType, obj.getWebsiteID());

            ParamDTO TenWS = new ParamDTO("TenWebsite",
                    DataTypeParam.StringType, obj.getTenWebsite());

            ParamDTO WebsiteLink = new ParamDTO("WebsiteLink",
                    DataTypeParam.StringType, obj.getWebsiteLink());
            ParamDTO Xoa = new ParamDTO("Xoa",
                    DataTypeParam.BooleanType, obj.getXoa());


            listParams.add(DanhMucID);
            listParams.add(TenWS);
            listParams.add(WebsiteLink);
            listParams.add(Xoa);
            //listParams.add(TenDanhMuc);
            //listParams.add(Xoa);

            isSuccess = helper.excuteNoneQueryUpdate("sp_Website_Update",
                    listParams);

            helper.CloseConnection();

        } catch (Exception ex) {
        }
        return isSuccess;
    }

    
    public List<Website_DTO> GetAll() {
        List<Website_DTO> listResult=new ArrayList<>();
        try {
            SqlDataHelper helper = new SqlDataHelper();
            helper.OpenConnection();

            ResultSet data = helper.excuteQuerySelect("sp_Website_GetAll");

            while (data.next()) {
                Website_DTO info = new Website_DTO();
                info.setWebsiteID(data.getInt("WebsiteID"));
                info.setTenWebsite(data.getString("TenWebsite"));
                info.setWebsiteLink(data.getString("WebsiteLink"));
                info.setXoa(data.getBoolean("Xoa"));

                listResult.add(info);
            }
            helper.CloseConnection();

        } catch (Exception ex) {
        }
        return listResult;
    }

   
    public Object SelectByPrimaryKey(int id) {
        Object objResult = null;
        try {
            SqlDataHelper helper = new SqlDataHelper();
            helper.OpenConnection();
            ParamDTO param = new ParamDTO("WebsiteID",
                    DataTypeParam.IntegerType, id);

            ResultSet data = helper.excuteQuerySelect("sp_Website_SelectByID", param);
            Website_DTO info = null;
            if (data.next()) {
                info = new Website_DTO();
                info.setWebsiteID(data.getInt("WebsiteID"));
                info.setTenWebsite(data.getString("TenWebsite"));
                info.setWebsiteLink(data.getString("WebsiteLink"));
                info.setXoa(data.getBoolean("Xoa"));
            }
            helper.CloseConnection();

        } catch (Exception ex) {
        }
        return objResult;
    }
}
